﻿using AntBlazorTableSample.Api.Models;
using AntBlazorTableSample.Common;
using Microsoft.AspNetCore.Mvc;

namespace AntBlazorTableSample.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CustomersController(CustomerStore customerStore) : ControllerBase
    {
        #region Public Methods

        [HttpGet]
        public IActionResult GetCustomers([FromQuery(Name = "page")] int pageNumber = 1,
            [FromQuery(Name = "size")] int pageSize = 10)
        {
            var result = new Paginated<Customer>
            {
                PageIndex = pageNumber,
                PageSize = pageSize,
                TotalCount = customerStore.Customers.Count,
                TotalPages = (int)Math.Ceiling((double)customerStore.Customers.Count / pageSize)
            };
            result.Items.AddRange(customerStore
                .Customers
                .OrderBy(c => c.FirstName)
                .Skip((pageNumber - 1) * pageSize)
                .Take(pageSize));
            return Ok(result);
        }

        #endregion Public Methods
    }
}